package com.atguigu.juc.one.work;


//资源类
class ShareData{
    //初始化数据
    private int num1 =  1;
    private int num2 = 65;
    int a,b;
    char c;
    int flag =0;

    //判断
    public synchronized void a() {
        if (num1>=1 && num1 <=52) {
            a= (num1);
            System.out.println(a);
            flag++;
            num1++;
            b= (num1);
            System.out.println(b);
            flag++;
            if(flag==2){
                flag=0;
                this.notifyAll();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        num1++;

        return;
    }

    public synchronized void b(){

        if(num2>=65 && num2 <=90){

            c = (char)(num2);
            System.out.println(c);
            flag++;
            if(flag==1){
                this.notifyAll();
                flag=0;
                try {

                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        num2++;
        return;
    }
}
public class WorkDemo {
    public static void main(String[] args) {
        ShareData data = new ShareData();
        new Thread(()->{
            for (int i = 0; i <26 ; i++) {
                data.a();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i <26 ; i++) {
                data.b();
            }
        }).start();
    }

}
